from flask import Blueprint,request,jsonify,make_response
from  flask import  current_app as app
import json
import os
import uuid
import  datetime
from utils.mylist import myfile
from utils.db import  db
from utils.comm import xtree
from utils.myes import Es
from utils.comm import  get_data
from utils.jwts import myjwt
from utils.myacl import acl
from utils.myrbac import rbac
from utils.redisconn import  r

bp_message = Blueprint('message',__name__)


#展示消息 然后添加消息
@bp_message.route('/getmessage/',methods=['POST','GET'])
async def getmessage():
    if request.method =='POST':
        #接收参数写入数据库
        data = get_data()
        # print(data)
        #添加到消息通知表中
        sql = "insert into message_list values('%s','%s','%s',%d,%d,%d,%d,'%s') "%(data['title'],data['content'],data['stime'],int(data['status']),int(data['type']),int(data['priority']),int(data['messagetupe']),data['etime'])
        db.insert(sql)
         
        #查询新添加的
        sql1 = "select * from message_list where stime='%s' and status=%d"(data['stime'],int(data['type']))
        res1 = db.find_one(sql1)
        #如果有传过来的用户列表 在用户列表页添加
        if int(res['type']) == 1 and res['stime'] == data['stime']:
            #默认未读
            for i in eval(data['idlst']):
                sql2 = "insert into user_message_list values(%d,%d,2)"%(int(res['id'],int(i)))
                db.insert(sql2)

        return jsonify({'code':200})
        
    else:
        userid = int(request.args.get('userid'))
        if userid == 4:
            #展示所有消息
            sql = "select * from message_list"
            res = db.find_all(sql)
            return jsonify({"code":200,'list':res})
        else:
            #展示用户消息   链表查询展示信息
            sql = "select ml.title,ml.content,ml.messagetype,ml.priority,ml.type,ml.status,ml.stime from message_list as ml left join user_message_list as uml on ml.id=uml.messageid where userid=%d"%(userid)
            res = db.find_all(sql)
            return jsonify({"code":200,'list':res})
















